﻿using IdentityModel.Client;
using JX.Core;
using JX.Core.Entity;
using JX.Dapper;
using JX.Infrastructure.Common;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;

namespace ConsoleTest
{
	class Program
	{
		static async Task Main(string[] args)
		{
			#region ids4客户端调用
			//var client = new HttpClient();
			//var disco = await client.GetDiscoveryDocumentAsync("http://localhost:9000");
			//if (disco.IsError)
			//{
			//    Console.WriteLine(disco.Error);
			//    Console.ReadLine();
			//    return;
			//}

			//var tokenResponse = await client.RequestClientCredentialsTokenAsync(new ClientCredentialsTokenRequest
			//{
			//    Address = disco.TokenEndpoint,
			//    ClientId = "TestApi",
			//    ClientSecret = "TestApi",
			//    Scope = "TestApi"
			//});

			//var tokenResponse = await client.RequestPasswordTokenAsync(new PasswordTokenRequest
			//{
			//    Address = disco.TokenEndpoint,
			//    ClientId = "JXWebApi",
			//    ClientSecret = "JXWebApi",
			//    Scope = "JXWebApi offline_access",
			//    UserName = "admin",
			//    Password = "admin888"
			//});
			//if (tokenResponse.IsError)
			//{
			//    Console.WriteLine(tokenResponse.Error);
			//    Console.ReadLine();
			//    return;
			//}
			//Console.WriteLine(tokenResponse.Json);
			//Console.WriteLine("\n\n");
			////call api
			//var apiClient = new HttpClient();
			//apiClient.SetBearerToken(tokenResponse.AccessToken);
			//var response = await apiClient.GetAsync("http://192.168.1.12:5000/api/test/getinfo");
			//if (!response.IsSuccessStatusCode)
			//{
			//    Console.WriteLine(response.StatusCode);
			//}
			//else
			//{
			//    var content = await response.Content.ReadAsStringAsync();
			//    Console.WriteLine(content);
			//}
			#endregion

			#region Lambda表达式转换成SQL语句
			//string temp = "10";
			//decimal temp1 = 10;
			//double temp2 = 10;
			//float temp3 = 10;
			//char temp4 = '1';
			//bool temp5 = false;
			//bool temp6 = true;
			//Guid guid = Guid.NewGuid();
			//Guid guid1 = Guid.NewGuid();
			//var modelType = (int)ModelType.Content;
			//List<SqlParaModel> listSqlParaModel = new List<SqlParaModel>();
			//Expression<Func<ModelsEntity, bool>> funcWhere = w => w.ModelType== modelType;

			//Expression<Func<LogEntity, bool>> funcWhere = w => true;
			//funcWhere = funcWhere.And(w => w.LogID == 15 && w.UserName == "admin");
			//funcWhere = funcWhere.And(w => w.LogID.In(new int[] { 15, 16, 17 }) && w.UserName.NotIn(new string[] { "a", "b", "c" }));
			//funcWhere = funcWhere.And(w => w.Title.Like("%aaa%") && w.Title.NotLike("bbb"));
			//funcWhere = funcWhere.And(w => w.Source.Contains("1"));
			//funcWhere = funcWhere.And(w => LambdaToSqlHelper.DateDiff("yyyy", DateTime.Now, DateTime.Now) >= 10);
			//funcWhere = funcWhere.And(w => w.LogID == temp.ToInt32(0));
			//funcWhere = funcWhere.And(w => w.LogID == temp1.ToDecimal(0));
			//funcWhere = funcWhere.And(w => w.LogID == temp2.ToDouble(0));
			//funcWhere = funcWhere.And(w => w.LogID == temp3.ToSingle(0));
			//funcWhere = funcWhere.And(w => w.LogID == temp4.ToChar());
			//funcWhere = funcWhere.And(w => temp6==temp5.ToBoolean() );
			//funcWhere = funcWhere.And(w => guid1 == guid.ToGuid());
			//Console.WriteLine(funcWhere.GetWhereSql(listSqlParaModel));
			//listSqlParaModel.ForEach(item =>
			//{
			//	Console.WriteLine(item.name + ":" + item.value);
			//});

			//Expression<Func<IQueryable<LogEntity>, IOrderedQueryable<LogEntity>>> orderBy = w => w.OrderByDescending(item=>item.Timestamp).ThenBy(item=>item.Title);
			//var sqlOrderBy = LambdaToSqlHelper.GetOrderBySql(orderBy);
			//Console.WriteLine(sqlOrderBy);

			//Expression<Func<LogEntity, int?>> funcSelect = w => w.LogID;
			////Expression<Func<LogEntity, object>> funcSelect = w => new { w.Title,w.ScriptName,Now="Now" };
			//var sqlSelect = LambdaToSqlHelper.GetQueryField(funcSelect);
			//Console.WriteLine(sqlSelect);

			//List<int?> list = new List<int?>();
			//list.Add(0);
			//list.Add(1);
			//foreach (int item in list)
			//{
			//	Console.WriteLine(item);
			//}
			#endregion

			#region Dapper
			//var contextOptions = new DapperDBContextOptions();
			//contextOptions.Configuration = "server=.;database=JXIPS3_1;uid=sa;pwd=abc123!@#;MultipleActiveResultSets=true;";
			//var myDBContext = new MyDapperDBContext(contextOptions);

			//string strSQL = "SELECT * FROM Users U INNER JOIN UserGroups G ON U.GroupID=G.GroupID WHERE U.GroupID=@GroupID";
			//var result = myDBContext.Query<UsersEntity, UserGroupsEntity, UserGroupsEntity>(strSQL, (u, g) => { return g; }, new { GroupID = 1}, splitOn: "GroupID");
			//Console.WriteLine(result.ToJson());

			//IModelsRepositoryDapper modelsRepositoryDapper = new ModelsRepositoryDapper(myDBContext, new DapperDBContextFactory());
			//IModelsServiceAppDapper modelsService = new ModelsServiceAppDapper(modelsRepositoryDapper);
			//var result = modelsService.AddDefaultField();
			//Console.WriteLine(result);

			//IAdminRepositoryDapper adminRepositoryDapper = new AdminRepositoryDapper(myDBContext, new DapperDBContextFactory());
			//var adminEntity = adminRepositoryDapper.Get(p => LambdaToSqlHelper.DateDiff("d", p.LoginTime, DateTime.Now) <= 7);
			//var adminEntity = adminRepositoryDapper.Get(p => true && p.IsLock==false);
			//Console.WriteLine(adminEntity.ToJson());

			//IRegionRepositoryDapper regionRepositoryDapper = new RegionRepositoryDapper(myDBContext,new DapperDBContextFactory());
			//var result = regionRepositoryDapper.GetScalar<string>(m=>m.Province,m=>m.RegionID==25,m=>m.OrderBy(item=>item.Province).ThenBy(item=>item.City));
			//Console.WriteLine(result);

			//var result1 = regionRepositoryDapper.GetScalar<dynamic>(m => new { m.Province , m.City}, m => m.RegionID == 25, m => m.OrderBy(item => item.Province).ThenBy(item => item.City));
			//Console.WriteLine(result1.Province + ":" + result1.City);

			//var result2 = regionRepositoryDapper.GetScalar<RegionEntity>(m => m, m => m.RegionID == 25, m => m.OrderBy(item => item.Province).ThenBy(item => item.City));
			//Console.WriteLine(result2.ToJson());

			//var result3 = regionRepositoryDapper.GetCount(m => m.RegionID > 0);
			//Console.WriteLine(result3);

			//var result4 = regionRepositoryDapper.IsExist(m => m.RegionID > 0);
			//Console.WriteLine(result4);

			//var result5 = regionRepositoryDapper.Get(m => m.RegionID == 10);
			//Console.WriteLine(result5.ToJson());

			//var result6 = regionRepositoryDapper.GetList(m => m.RegionID >= 10 && m.RegionID <= 15,m=>m.OrderBy(item=>item.RegionID));
			//result6.ToList().ForEach(item =>
			//{
			//	Console.WriteLine(item.ToJson());
			//});

			//var result7 = regionRepositoryDapper.GetAllData<RegionEntity, RegionEntity>(m => m.RegionID >= 20 && m.RegionID <= 25, m => m.OrderBy(item => item.RegionID),m=>m);
			//result7.ToList().ForEach(item =>
			//{
			//	Console.WriteLine(item.ToJson());
			//});

			//var result8 = adminRepositoryDapper.GetBySQL<string>("select TableName from Models where ModelType=@ModelType", new { ModelType = ModelType.UserOption });
			//Console.WriteLine(result8);

			#endregion

			Console.ReadLine();
		}
	}
}
